/*  File Name	: FirewallSetting.cpp
 *  Author		: Javie Pong
 *	 E-mail		: 823960168@qq.com
 *  Created on	: 2012-10-1
 *  Description:
 */
#include <iostream>
#include "FirewallSetting.h"
#include "MikrotikAPI.h"
#include "MikrotikAPITypes.h"

FirewallSetting::FirewallSetting()
{
}

FirewallSetting::~FirewallSetting()
{
}

int FirewallSetting::add_address_list(std::string ip,std::string limit_ip)
{
	Sentence readSentence;
	Sentence writeSentence;
	writeSentence.AddWord("/ip/firewall/address-list/add");
	writeSentence.AddWord("=list="+ip);
	writeSentence.AddWord("=comment="+ip);
	writeSentence.AddWord("=address="+limit_ip);
	writeSentence.AddWord("=disabled=no");

	MikrotikAPI::instance()->WriteSentence(writeSentence);

	MikrotikAPI::instance()->ReadSentence(readSentence);
	readSentence.Print();
	return 0;
}

int FirewallSetting::remove_address_list(std::string ip)
{
		StrVector vec;
		vec.clear();
		find_addrees_list(ip,vec);
		if(vec.size()<=0)
		{
			return 0;
		}
		for(StrVector::iterator iter=vec.begin();iter!=vec.end();iter++)
		{
			std::cout << "-------remove id:"<<*iter<< std::endl;
			Sentence readSentence;
			Sentence writeSentence;
			writeSentence.AddWord("/ip/firewall/address-list/remove");
			writeSentence.AddWord("=.id="+*iter);
			RosAPI->WriteSentence(writeSentence);
			RosAPI->ReadSentence(readSentence);
			readSentence.Print();
		}
	return 0;
}

int FirewallSetting::find_addrees_list(std::string ip,StrVector &vec)
{
	std::cout << "-------find:"<<ip<< std::endl;
	std::string id = "";
	Sentence readSentence;
	Sentence writeSentence;
	writeSentence.AddWord("/ip/firewall/address-list/print");
	writeSentence.AddWord("?comment=" + ip);
	RosAPI->WriteSentence(writeSentence);
	while (true)
	{
		RosAPI->ReadSentence(readSentence);
		StrMap map;
		readSentence.GetMap(map);
		if(map.size()<=0)
		{
			break;
		}
		for (StrMap::iterator iter = map.begin(); iter != map.end(); iter++)
		{
			if(iter->first!="")
			{
				vec.push_back(iter->first);
			}
			std::cout << iter->first << "-->" << iter->second << "\t";
		}
		std::cout << std::endl;

	}
	return 0;
}

int FirewallSetting::print_addrees_list(std::string ip,bool all)
{
	std::cout << "-------print:"<<ip<< std::endl;

	Sentence readSentence;
	Sentence writeSentence;
	writeSentence.AddWord("/ip/firewall/address-list/print");
	if(!all)
	{
		writeSentence.AddWord("?list=" + ip);
	}

	RosAPI->WriteSentence(writeSentence);

	while(true)
	{
		RosAPI->ReadSentence(readSentence);
		StrMap map;
		readSentence.GetMap(map);
		if(map.size()<=0)
		{
			break;
		}
		for (StrMap::iterator iter = map.begin(); iter != map.end(); iter++)
		{
			std::cout << iter->first << "-->" << iter->second << "\t";
		}
		std::cout << std::endl;
	}
	return 0;
}


int FirewallSetting::add_filter(std::string chain,std::string src_ip,std::string dst_ip_list)
{
	Sentence readSentence;
	Sentence writeSentence;
	writeSentence.AddWord("/ip/firewall/filter/add");
	writeSentence.AddWord("=chain="+chain);
	writeSentence.AddWord("=src-address="+src_ip);
	writeSentence.AddWord("=dst-address-list="+dst_ip_list);
	writeSentence.AddWord("=action=reject");
	writeSentence.AddWord("=reject-with=icmp-host-prohibited");
	writeSentence.AddWord("=disabled=no");
	writeSentence.AddWord("=comment="+src_ip);

	MikrotikAPI::instance()->WriteSentence(writeSentence);

	MikrotikAPI::instance()->ReadSentence(readSentence);
	readSentence.Print();
	return 0;
}

int FirewallSetting::remove_filter(std::string ip)
{
	StrVector vec;
	vec.clear();
	find_filter(ip,vec);
	if(vec.size()<=0)
	{
		return 0;
	}
	for(StrVector::iterator iter=vec.begin();iter!=vec.end();iter++)
	{
		std::cout << "-------remove id:"<<*iter<< std::endl;
		Sentence readSentence;
		Sentence writeSentence;
		writeSentence.AddWord("/ip/firewall/filter/remove");
		writeSentence.AddWord("=.id="+*iter);
		RosAPI->WriteSentence(writeSentence);
		RosAPI->ReadSentence(readSentence);
		readSentence.Print();
	}
return 0;
}

int FirewallSetting::find_filter(std::string ip,StrVector &vec)
{
	std::string id = "";
	Sentence readSentence;
	Sentence writeSentence;
	writeSentence.AddWord("/ip/firewall/filter/print");
	writeSentence.AddWord("?src-address=" + ip);
	RosAPI->WriteSentence(writeSentence);
	while (true)
	{
		RosAPI->ReadSentence(readSentence);
		StrMap map;
		readSentence.GetMap(map);
		if(map.size()<=0)
		{
			break;
		}
		for (StrMap::iterator iter = map.begin(); iter != map.end(); iter++)
		{
			if(iter->first==".id")
			{
				vec.push_back(iter->second);
			}
			std::cout << iter->first << "-->" << iter->second << "\t";
		}
		std::cout << std::endl;

	}
	return 0;
}
